<?php
namespace TbfDocument;
class FieldProcessor {
    static function ListString(Context $c,$fieldName){
        return $c->obj[$fieldName];
    }
    static function FormString(Context $c,$fieldName){
        $config = $c->getFieldByName($fieldName);
        if (isset($c->obj[$fieldName])){
            $config['Value'] = $c->obj[$fieldName];
        }
        return $config;
    }
    //数据已经放到里面了$obj,这个是默认行为,此处什么也不做
    static function ActionString(Context $c,$fieldName){
    }
    static function ListNone(Context $c,$fieldName){
        return null;
    }
    static function FormNone(Context $c,$fieldName){
        return null;
    }
    static function FormHidden(Context $c,$fieldName){
        if (isset($c->obj[$fieldName])){
            $value = $c->obj[$fieldName];
        }else{
            $value = '';
        }
        return array(
            'Type'=>'Hidden',
            'Name'=>$fieldName,
            'Value'=>$value,
        );
    }
    static function ActionNone(Context $c,$fieldName){
        unset($c->obj[$fieldName]);
    }
    static function FormNeed(Context $c,$fieldName){
        $config = self::FormString($c,$fieldName);
        $config['Need'] = true;
        return $config;
    }
    static function ActionNeed(Context $c,$fieldName){
        if (empty($c->obj[$fieldName])){
            $config = $c->getFieldByName($fieldName);
            \TbfError::client(sprintf('请输入[%s]',$config['ShowName']));
            return;
        }
    }
    static function FormDefault(Context $c,$fieldName,$default){
        $config = $c->getFieldByName($fieldName);
        if (!isset($c->obj[$fieldName])){
            $config['Value'] = $default;
        }else{
            $config['Value'] = $c->obj[$fieldName];
        }
        return $config;
    }
    static function FormDefaultDateTimeNow(Context $c,$fieldName){
        $config =  $c->getFieldByName($fieldName);
        if (empty($c->obj[$fieldName]) || $c->obj[$fieldName]==='0000-00-00 00:00:00'){
            $config['Value'] = date('Y-m-d H:i:s');
        }else{
            $config['Value'] = $c->obj[$fieldName];
        }
        return $config;
    }

    //这个函数破坏了封装性,此处不应该出现任何ci的接口
    static function FormDefaultStatus(Context $c,$fieldName){
        $config = $c->getFieldByName($fieldName);
        if (!isset($c->obj[$fieldName])){
            $config['Value'] =  \TbfSession::get('LastDocumentStatus');
        }else{
            $config['Value'] = $c->obj[$fieldName];
        }
        return $config;

    }
}
